import Vue from 'vue'
import store from '@/store'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import index from '../views/index.vue'

Vue.use(VueRouter);

import {Toast} from 'vant';

const routes = [
  {
    path: '/',
    name: 'index',
    component: index,
	children:[{
		path: '/',
		name: 'Home',
		component: Home
	},{
		path: '/publish',
		name: 'publish',
		component:  () => import('../views/publish.vue')
	},{
		path: '/my',
		name: 'my',
		component:  () => import('../views/my.vue')
	}]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('../views/reg.vue')
  },
  {
    path: '/mylogin',
    name: 'mylogin',
    component: () => import('../views/mylogin.vue')
  },
  {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import('../views/userInfo.vue'),
	// meta:{
	// 	check:true
	// },
  },
  {
    path: '/upload',
    name: 'upload',
    component: () => import('../views/upload.vue')
  },
  {
    path: '/edit_password',
    name: 'edit_password',
    component: () => import('../views/edit_password.vue'),
	meta:{
		check:true
	}
  },
  {
    path: '/contact',
    name: 'contact',
    component: () => import('../views/contact.vue'),
	meta:{
		check:true
	}
	},
	{
	  path: '/my_publish',
	  name: 'my_publish',
	  component: () => import('../views/my_publish.vue'),
		meta:{
			check:true
		}
  },
  {
  	  path: '/search',
  	  name: 'search',
  	  component: () => import('../views/search.vue'),
  },
  {
  	  path: '/search_list',
  	  name: 'search_list',
  	  component: () => import('../views/search_list.vue'),
  },
  {
  	  path: '/article',
  	  name: 'article',
  	  component: () => import('../views/article.vue'),
  		// meta:{
  		// 	check:true
  		// }
  },
  

]

const router = new VueRouter({
  routes
});



// 白名单  
 
 let  table = ['/login','/my'];  
 
 const originalPush = VueRouter.prototype.push
 VueRouter.prototype.push = function push(location, onResolve, onReject) {
     if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
     return originalPush.call(this, location).catch(err => err)
 }
 const originalReplace = VueRouter.prototype.replace
 VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
     if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
     return originalReplace.call(this, location).catch(err => err)
 }
 

router.beforeEach((to,from,next)=>{
	// let token = store.state.TOKEN;
	
	let token = localStorage.getItem('token');
	if(token){
		// 证明登录了
		next()
		
	}else{
		// 没登录
		// to.meta&&to.meta.check  r如果页面需要验证
		if(to.meta&&to.meta.check){
			console.log('没登录，进不去')
			Toast('您还未登录,请先登录');
			
			setTimeout(v=>{
				router.push('/login')
			},500)
			return
			// next({
			// 	path:'/login',
			// 	replace:true
			// })
			
		}else{
			next()
			
		}
		
		
	}
	
})



export default router
